package com.koicarp.university.graduate.service.service.role.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koicarp.university.graduate.service.dao.SysRoleDao;
import com.koicarp.university.graduate.service.dao.SysRoleMenuRelaDao;
import com.koicarp.university.graduate.service.entity.SysRole;
import com.koicarp.university.graduate.service.entity.SysRoleMenuRela;
import com.koicarp.university.graduate.service.service.role.ISysRoleService;
import com.koicarp.university.graduate.service.vo.pesonalManage.SysRoleInsVo;
import com.koicarp.university.graduate.service.vo.pesonalManage.SysRoleVo;
import com.koicarp.university.graudate.common.constant.BaseConstant;
import com.koicarp.university.graudate.common.utils.UniversityResponse;
import com.koicarp.university.graudate.common.utils.UniversityUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 【用户管理：角色表】(SysRole)表服务实现类
 *
 * @author liutao
 * @since 2020-07-30 17:00:34
 */
@Slf4j
@RequiredArgsConstructor
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao,SysRole> implements ISysRoleService {
    private final SysRoleDao sysRoleDao;

    private final SysRoleMenuRelaDao sysRoleMenuRelaDao;

    @Override
    public UniversityResponse getSysRoleWithPage(SysRoleVo vo) {
        UniversityResponse response = new UniversityResponse();
        Page<SysRole> page = null;
        boolean findOne = UniversityUtil.isNotNull(vo.getId());
        boolean isPage = UniversityUtil.isNotNull(vo.getPageNum())
                && UniversityUtil.isNotNull(vo.getPageSize());
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(vo,sysRole);
        if (!isPage && !findOne){
            response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
            Map listRes = new HashMap();
            listRes.put(BaseConstant.RESPONSE_RESULTSET,this.sysRoleDao.queryAll(sysRole));
            response.data(listRes);
            return response;
        }
        if (findOne){
            page = new Page<>(1,1,false);
        } else{
            page = new Page<>(vo.getPageNum(),vo.getPageSize(),true);
        }
        IPage<SysRole> result = null;
        result = sysRoleDao.queryRoleWithPage(page, vo);
        if (findOne){
            List<SysRole> list = result.getRecords();
            if (list == null || list.size() == 0){
                response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
                response.message(BaseConstant.RESPONSE_MESSAGE_OPR_RESULT_NULL);
            } else {
                response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
                response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
                response.data(list.get(0));
            }
        }else{
            response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
            Map<String, Object> dataTable = UniversityUtil.getDataTable(result);
            response.data(dataTable);
        }
        return response;
    }

    @Override
    public UniversityResponse getMenuIdByRoleId(SysRoleMenuRela sysRoleMenuRela) {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        LambdaQueryWrapper<SysRoleMenuRela> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleMenuRela::getRoleId,sysRoleMenuRela.getRoleId());
        List<SysRoleMenuRela> list = sysRoleMenuRelaDao.selectList(wrapper);
        response.data(list);
        return response;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysRole queryById(Long id) {
        return this.sysRoleDao.queryById(id);
    }

    @Override
    public UniversityResponse getAllSysRole() {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        LambdaQueryWrapper<SysRole> wrapper =new LambdaQueryWrapper<>();
        List<SysRole> list = sysRoleDao.selectList(wrapper);
        response.data(list);
        return response;
    }

    @Override
    public SysRole queryByRoleId(String roleId) {
        return this.sysRoleDao.queryByRoleId(roleId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<SysRole> queryAllByLimit(int offset, int limit) {
        return this.sysRoleDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param sysRole 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public UniversityResponse insert(SysRoleInsVo vo) {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(vo,sysRole);
        //查找一下有没有同样的roleId
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getRoleId,vo.getRoleId());
        List<SysRole> list = this.sysRoleDao.selectList(wrapper);
        if (list.size() > 0 ){
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message("角色编码已经存在");
            return response;
        }
        this.sysRoleDao.insert(sysRole);
        List<SysRoleMenuRela> sysRoleMenuRelaList = new ArrayList<>();
        for (int j = 0; j < vo.getMenuIdArr().length; j++) {
            SysRoleMenuRela sysRoleMenuRela = new SysRoleMenuRela();
            sysRoleMenuRela.setRoleId(sysRole.getRoleId());
            sysRoleMenuRela.setMenuId(vo.getMenuIdArr()[j]);
            sysRoleMenuRelaList.add(sysRoleMenuRela);
        }
        int i = this.sysRoleMenuRelaDao.insertList(sysRoleMenuRelaList);
        if (i < 0){
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
            return response;
        }
        return response;
    }


    /**
     * 修改数据
     *
     * @param sysRole 实例对象
     * @return 实例对象
     */
    @Override
    public SysRole update(SysRole sysRole) {
        this.sysRoleDao.update(sysRole);
        return this.queryById(sysRole.getId());
    }

    @Override
    @Transactional
    public UniversityResponse modifySysRole(SysRoleInsVo vo) {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(vo,sysRole);

        this.sysRoleDao.update(sysRole);
        List<SysRoleMenuRela> sysRoleMenuRelaList = new ArrayList<>();
        for (int j = 0; j < vo.getMenuIdArr().length; j++) {
            SysRoleMenuRela sysRoleMenuRela = new SysRoleMenuRela();
            sysRoleMenuRela.setRoleId(sysRole.getRoleId());
            sysRoleMenuRela.setMenuId(vo.getMenuIdArr()[j]);
            sysRoleMenuRelaList.add(sysRoleMenuRela);
        }
        //先批量删除 再批量添加
        LambdaQueryWrapper<SysRoleMenuRela> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(SysRoleMenuRela::getRoleId,sysRole.getRoleId());
        sysRoleMenuRelaDao.delete(wrapper1);
        int i = this.sysRoleMenuRelaDao.insertList(sysRoleMenuRelaList);
        if (i < 0){
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
            return response;
        }
        return response;
    }

    @Override
    @Transactional
    public UniversityResponse removeSysRole(SysRole sysRole) {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getRoleId,sysRole.getRoleId());
        sysRoleDao.delete(wrapper);
        LambdaQueryWrapper<SysRoleMenuRela> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(SysRoleMenuRela::getRoleId,sysRole.getRoleId());
        int i = sysRoleMenuRelaDao.delete(wrapper1);
        if (i < 0){
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.sysRoleDao.deleteById(id) > 0;
    }
}